package com.manage.utils;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.charset.Charset;

/**
 * 图片压缩工具
 *
 * @author Fitch
 */
public class ThumbnailatorTest {
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        ThumbnailatorTest thumbnailatorTest = new ThumbnailatorTest();
        //thumbnailatorTest.test2();
        //thumbnailatorTest.test2();
        //thumbnailatorTest.test3();
        //thumbnailatorTest.test4();
        //thumbnailatorTest.test6();
        //thumbnailatorTest.test7();
        //.test8();
        String url = "C:\\Users\\Fitch\\Desktop\\新建文本文档.txt";
        String url1 = "C:\\Users\\Fitch\\Desktop\\题目_1.0\\A01视注意\\图中找不同\\图中找不同(1)待确认\\难度4";
        //readFileByLines3(url);
        dealPicture(url1, url1, 0.8f);


    }
    /**
     * 61.      * 以行为单位读取文件，常用于读面向行的格式化文件
     * <p>
     * 62.
     */

    public static void readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            String temp1="",temp2="";
            int id = 15160134;
            int id1 = 14481;
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                //System.out.println("line " + line + ": " + tempString);
                if(line%2>0){
                     temp1 = "INSERT INTO `k_material_detail` (`id`, `content`, `status`, `create_time`, `create_user`, `update_time`, `update_user`, `remarks`) VALUES ('"+id+"', '{\\\"name\\\":\\\""+tempString.split(":")[1];
                }else if(line%2==0){
                    String[] strTemp = tempString.split(" ");
                    String content ="[";
                    for(int i=0;i<strTemp.length;i++){
                        content=content+"\\\""+strTemp[i]+"\\\"";
                        if(i<strTemp.length-1){
                            content = content+",";
                        }
                        if(i==strTemp.length-1){
                            content = content+"]";
                        }

                    }
                    temp2 =temp1+"\\\", \\\"content\\\": "+content+"}', '0', now(), '0', now(), '0', NULL);";

                    //System.out.println(temp2);
                    temp1=null;
                    temp2=null;

                    temp1 ="INSERT INTO `k_subject_material` (`id`, `material_detail_id`, `project_type`, `project_code`, `start_age`, `end_age`, `start_grade`, `end_grade`, `level`, `status`, `create_user`, `create_time`, `update_user`, `update_time`, `remarks`) VALUES ('"+id1+"', '"+id+"', '13', '1316', '12', '14', NULL, NULL, NULL, '0', '0', now(), '0', now(), NULL);";
                   System.out.println(temp1);
                    temp1=null;
                    id++;
                    id1++;
                }
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    public static void readFileByLines1(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            String temp1="",temp2="";
            int id1 = 15160304;
            int id2 = 14651;
            while ((tempString = reader.readLine()) != null) {
                temp1 = "INSERT INTO `k_material_detail` (`id`, `content`, `status`, `create_time`, `create_user`, `update_time`, `update_user`, `remarks`) VALUES ('"+id1+"', '"+tempString+"', '0', now(), '0', now(), '0', NULL);";

                temp2 = "INSERT INTO `k_subject_material` (`id`, `material_detail_id`, `project_type`, `project_code`, `start_age`, `end_age`, `start_grade`, `end_grade`, `level`, `status`, `create_user`, `create_time`, `update_user`, `update_time`, `remarks`) VALUES ('"+id2+"', '"+id1+"', '13', '1308', '12', '14', '1', '9', NULL, '0', '0', now(), '0', now(), NULL);";
                System.out.println(temp1);
                System.out.println(temp2);
                line++;
                id1++;
                id2++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    public static void readFileByLines2(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            String temp1="",temp2="";
            int id = 15160429;
            int id1 = 14776;
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                if(line%2>0){
                    temp1 = "INSERT INTO `k_material_detail` (`id`, `content`, `status`, `create_time`, `create_user`, `update_time`, `update_user`, `remarks`) VALUES ('"+id+"', '"+tempString+"\\n";
                }else if(line%2==0){
                    String[] strTemp = tempString.split("，");
                    String content ="";
                    for(int i=0;i<strTemp.length;i++){
                        content=content+strTemp[i];
                        if(i<strTemp.length-1){
                            content = content+"，\\n";
                        }
                        if(i==strTemp.length-1){
                            content = content+"\\n";
                        }

                    }
                    temp2 =temp1+content+"', '0', now(), '0', now(), '0', NULL);";

                    System.out.println(temp2);
                    temp1=null;
                    temp2=null;

                    temp1 ="INSERT INTO `k_subject_material` (`id`, `material_detail_id`, `project_type`, `project_code`, `start_age`, `end_age`, `start_grade`, `end_grade`, `level`, `status`, `create_user`, `create_time`, `update_user`, `update_time`, `remarks`) VALUES ('"+id1+"', '"+id+"', '12', '1206', '12', '14', '1', '9', NULL, '0', '0', now(), '0', now(), NULL);";
                    System.out.println(temp1);
                    temp1=null;
                    id++;
                    id1++;
                }
                line++;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    public static void readFileByLines3(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        try {
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 1;
            // 一次读入一行，直到读入null为文件结束
            String temp="";
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                temp = "INSERT INTO `ies_oies_test`.`k_material_detail` (`project_type_code`, `project_code`, `project_difficulty`, `content`, `config`, `status`, `remarks`, `create_time`, `create_user`, `update_time`, `update_user`) VALUES ('A12', 'A1207', '0', '"+tempString+"', NULL, '0', '', NOW(), '0', NOW(), '0');\n";
                line++;
                System.out.println(temp);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
    }

    /**
     * 图片压缩
     * @param sourceUrl
     * @param objectUrl
     * @param scale
     * @return
     */
    private static boolean dealPicture(String sourceUrl, String objectUrl, float scale) {
        File file = new File(sourceUrl);
        File[] files = file.listFiles();
        for (File f : files) {
            File temp = new File(f.toString());
            if (temp.isDirectory()) {
                dealPicture(f.toString(), objectUrl + StrUtil.split(f.toString(), sourceUrl)[1], scale);
            } else {
                if (f.toString().indexOf(".jpg") > 0 || f.toString().indexOf(".png") > 0 || f.toString().indexOf(".jpeg") > 0 || f.toString().indexOf(".bmp") > 0) {
                    OutputStream os = null;
                    try {
                        String sourceStr = f.toString();
                        String oldFileName = sourceStr;
                        String fileName= StrUtil.split(f.toString(), sourceUrl)[1];
                        fileName = fileName.replace("@3x","");
                        fileName = fileName.replace("@2x","");
                        fileName = fileName.replace("jpg","png");
                        fileName = fileName.trim();
                        String objectStr = objectUrl + fileName;
                        File objectDir = new File(objectUrl);
                        if (!objectDir.exists()) {
                            objectDir.mkdirs();
                        }
                        Thumbnails.of(sourceStr).scale(scale).toFile(objectStr);
                        System.out.println(f.toString());
                        if(!oldFileName.equals(objectStr)){
                            File file1 = new File(oldFileName);
                            if(file1.exists()){
                                if(file1.isFile()){
                                    file1.delete();
                                }
                            }

                        }
                    } catch (Exception e) {
                        return false;
                    }
                }
            }
        }
        return true;

    }


    /**
     * 指定大小进行缩放
     *
     * @throws IOException
     */
    private void test1() throws IOException {
        /*
         * size(width,height) 若图片横比200小，高比300小，不变
         * 若图片横比200小，高比300大，高缩小到300，图片比例不变 若图片横比200大，高比300小，横缩小到200，图片比例不变
         * 若图片横比200大，高比300大，图片按比例缩小，横为200或高为300
         */
        Thumbnails.of("C:\\fhc\\A2.jpg").size(200, 300).toFile("C:\\fhc\\image_200x300.jpg");
        Thumbnails.of("C:\\fhc\\A2.jpg").size(2560, 2048).toFile("C:\\fhc\\image_2560x2048.jpg");
    }



    /**
     * 按照比例进行缩放
     *
     * @throws IOException
     */
    private void test2() throws IOException {
        /**
         * scale(比例)
         */
        Thumbnails.of("C:\\fhc\\A2.jpg").scale(0.25f).toFile("C:\\fhc\\image_25%.jpg");
        Thumbnails.of("C:\\fhc\\A2.jpg").scale(1.10f).toFile("C:\\fhc\\image_110%.jpg");
        Thumbnails.of("C:\\fhc\\A2.jpg").scale(1.00f).toFile("C:\\fhc\\image_100%.jpg");

    }

    /**
     * 不按照比例，指定大小进行缩放
     *
     * @throws IOException
     */
    private void test3() throws IOException {
        /**
         * keepAspectRatio(false) 默认是按照比例缩放的
         */
        Thumbnails.of("C:\\fhc\\A2.jpg").size(120, 120).keepAspectRatio(false).toFile("C:\\fhc\\image_120x120.jpg");
    }

    /**
     * 旋转
     *
     * @throws IOException
     */
    private void test4() throws IOException {
        /**
         * rotate(角度),正数：顺时针 负数：逆时针
         */
        Thumbnails.of("C:\\fhc\\A2.jpg").size(1280, 1024).rotate(90).toFile("C:\\fhc\\images+90.jpg");
        Thumbnails.of("C:\\fhc\\A2.jpg").size(1280, 1024).rotate(-90).toFile("C:\\fhc\\iamge-90.jpg");
    }


    /**
     * 裁剪
     *
     * @throws IOException
     */
    private void test6() throws IOException {
        /**
         * 图片中心400*400的区域
         */
        Thumbnails.of("C:\\fhc\\A2.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false)
                .toFile("C:\\fhc\\image_region_center.jpg");
        /**
         * 图片右下400*400的区域
         */
        Thumbnails.of("C:\\fhc\\A2.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false)
                .toFile("C:\\fhc\\image_region_bootom_right.jpg");
        /**
         * 指定坐标
         */
        Thumbnails.of("C:\\fhc\\A2.jpg").sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false).toFile("C:\\fhc\\image_region_coord.jpg");
    }

    /**
     * 转化图像格式
     *
     * @throws IOException
     */
    private void test7() throws IOException {
        /**
         * outputFormat(图像格式)
         */
        Thumbnails.of("C:\\fhc\\A2.jpg").size(1280, 1024).outputFormat("png").toFile("C:\\fhc\\image_1280x1024.png");
        Thumbnails.of("C:\\fhc\\A2.jpg").size(1280, 1024).outputFormat("gif").toFile("C:\\fhc\\image_1280x1024.gif");
    }

    /**
     * 输出到OutputStream
     *
     * @throws IOException
     */
    private void test8() throws IOException {
        /**
         * toOutputStream(流对象)
         */
        OutputStream os = new FileOutputStream("C:\\fhc\\image_1280x1024_OutputStream.png");
        Thumbnails.of("C:\\fhc\\A2.jpg").size(1280, 1024).toOutputStream(os);
    }


}
